//
//  SYPhotoBrowserDismissAnimated.swift
//  SingWeibo
//
//  Created by shiying on 16/1/2.
//  Copyright © 2016年 shiying. All rights reserved.
//

import UIKit

class SYPhotoBrowserDismissAnimated: NSObject, UIViewControllerAnimatedTransitioning {
    
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        return 0.25
    }
    
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        let fromVC = transitionContext.viewControllerForKey(UITransitionContextFromViewControllerKey) as! SYPhotoBrowserViewController
        // 获取modal出来的控制器View
        let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
        
        let tempImageView = fromVC.dismissTempImageView() // 获取过渡视图
        // 添加到容器视图
        transitionContext.containerView()?.addSubview(tempImageView)
        // 隐藏collectionView
        fromVC.collectionView.hidden = true
        
        
        UIView.animateWithDuration(transitionDuration(transitionContext), animations: { () -> Void in
            fromView?.alpha = 0
            tempImageView.frame = fromVC.dismissTargetRect() //将过渡视图缩放目标
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
        }
    }
}
